package nsfs

import "GLibHac/fssystem"

type Aes128XtsStorage struct {
	*SectorStorage
	readTransform *Aes128XtsTransform
}

func (i *Aes128XtsStorage) Read(position int64, destination []byte) (int, error) {
	sectorIndex := position / int64(i.SectorSize)
	n, err := i.SectorStorage.Read(position, destination)
	if err != nil {
		return 0, err
	}
	i.readTransform.TransformBlock(destination, uint64(sectorIndex))
	return n, nil
}

func NewAes128XtsStorage(storage fssystem.IStorage, key1 []byte, key2 []byte, sectorSize int32, decrypted bool) (*Aes128XtsStorage, error) {
	reader := &Aes128XtsStorage{
		SectorStorage: NewSectorStorage(storage, sectorSize),
	}
	reader.ReadFunc = reader.Read
	transform, err := NewAes128XtsTransform(key1, key2, decrypted)
	if err != nil {
		return nil, err
	}
	reader.readTransform = transform
	return reader, nil
}
